//
//  YXHomeViewModel.m
//  CaoShi
//
//  Created by aa on 2018/5/3.
//  Copyright © 2018年 杨旭. All rights reserved.
//

#import "YXHomeViewModel.h"
#import "NetworkManager.h"
#import "YXHomeModel.h"
#import "WZHNavigationController.h"
#import "YXWebViewController.h"
#import "YXHomeCell.h"
@interface YXHomeViewModel ()
@property (nonatomic ,strong) WZHNavigationController *nav;
@property (nonatomic, strong) UIImageView *bgImg;
@property (nonatomic, strong) UIVisualEffectView *effectView;
@property (nonatomic ,strong) UIButton *cityBtn;
@property (nonatomic ,strong) UIButton *searchBtn;
@property (nonatomic ,strong) UIViewController *viewController;
@property (nonatomic ,assign) NSInteger page;
@end

@implementation YXHomeViewModel

+ (instancetype)model {
    YXHomeViewModel *model = [[self alloc] init];
    return model;
}

- (void)cellWithTableView:(UITableView *)tableView {
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.emptyDataSetDelegate = self;
    tableView.emptyDataSetSource = self;
    
    [tableView registerNib:[UINib nibWithNibName:@"YXHomeCell" bundle:nil] forCellReuseIdentifier:@"YXHomeCell"];
}
- (void)setNav:(WZHNavigationController *)nav {
    _nav = nav;
}

- (void)setBgImg:(UIImageView *)bgImg {
    _bgImg = bgImg;
}
- (void)setEffectView:(UIVisualEffectView *)effectView {
    _effectView = effectView;
}
- (void)setCityBtn:(UIButton *)cityBtn {
    _cityBtn = cityBtn;
}
- (void)setSearchBtn:(UIButton *)searchBtn {
    _searchBtn = searchBtn;
}

- (void)pushSupController:(UIViewController *)supController {
    self.viewController = supController;
}

- (void)loadData:(UITableView *)tableView {
    
    NSString *urlStr = [NSString stringWithFormat:@"%@%@",serverUrl,@"/v2/book/search"];
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    [param setObject:@(_page) forKey:@"start"];
    [param setObject:@"5" forKey:@"count"];
    [param setObject:@"图书" forKey:@"tag"];
    
    [NetworkManager requestPOSTWithURLStr:urlStr paramDic:param finish:^(id responseObject) {
        if (_page == 0) {
            [self.dataArr removeAllObjects];
        }
//        NSLog(@"%@",responseObject);
        NSArray *listArr = [YXHomeModel mj_objectArrayWithKeyValuesArray:responseObject[@"books"]];
        _page++;
        [self.dataArr addObjectsFromArray:listArr];
        [tableView reloadData];
        [tableView.mj_header endRefreshing];
        [tableView.mj_footer endRefreshing];
    } enError:^(NSError *error) {
        NSLog(@"%@",error);
        [tableView.mj_header endRefreshing];
        [tableView.mj_footer endRefreshing];
    }];
}

- (void)addRefrsh:(UITableView *)tableView {
    
    __weak typeof(self)weakSelf = self;
    tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        weakSelf.page = 0;
        [weakSelf loadData:tableView];
    }];
    tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        [weakSelf loadData:tableView];
    }];
    [tableView.mj_header beginRefreshing];
    
}

#pragma mark - UITableView Delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 80;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    YXHomeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YXHomeCell" forIndexPath:indexPath];
    cell.model = self.dataArr[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    YXWebViewController *vc = [[YXWebViewController alloc] init];
    [self.viewController.navigationController pushViewController:vc animated:YES];
    
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat offsetY = scrollView.contentOffset.y;
    CGFloat alpha = offsetY / 100;
    //    [self.navBar setBackgroundImage:[UIImage imageWithColor:[[UIColor py_colorWithHexString:@"#F4153D"] colorWithAlphaComponent:alpha] size:CGSizeMake(kScreenWidth, 64)] forBarMetrics:UIBarMetricsDefault];
    self.nav.w_barBgAlpha = alpha;
    
    self.nav.w_navBackgroundColor = [UIColor whiteColor];
    if (offsetY > 100) {
        [self.searchBtn setBackgroundColor:[[UIColor py_colorWithHexString:@"#444444"] colorWithAlphaComponent:0.3]];
    }else {
        [self.searchBtn setBackgroundColor:[[UIColor py_colorWithHexString:@"#f3f3f3"] colorWithAlphaComponent:0.3]];
    }
    
    CGFloat H = kScreenWidth / 16 * 9 + 10 - offsetY;
    
    if (offsetY > 0) {
        H = kScreenWidth / 16 * 9 + 10;
        
        // 设置模糊效果滚动起始位置
        CGRect rect = self.bgImg.frame;
        rect.origin.y = -offsetY;
        self.bgImg.frame = rect;
    }
    if (offsetY < 0){
        self.nav.w_navBarAlpha = 0;
    }else {
        self.nav.w_navBarAlpha = 1;
    }
    
    
//    self.bgImg.frame = CGRectMake(-5, -5, kScreenWidth + 10, H); 
    self.effectView.frame = self.bgImg.bounds;
}


@end
